<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
date_default_timezone_set ( 'prc' );
require_once (dirname ( __FILE__ ) . "/../../PHPExcel/PHPExcel.php");
class Staff extends CI_Controller {
	public function __construct() {
		parent::__construct ();
		if (! $this->session->userdata ( 'logged_in' )) {
			redirect ( 'index_admin/login' );
			exit ();
		}
	}
	/*
	 * 返回员工列表
	 */
	public function index() {
		$name = $this->uri->segment(4);
    	$name=urldecode($name);//转化为汉字
    	$department = $this->uri->segment(6);
		$type = $this->uri->segment(8);
		$security = $this->uri->segment(10);
		$fund = $this->uri->segment(12);
		$state = $this->uri->segment(14);
		
    	if(empty($name)){//这里是为了将name加到url上,虽然前台也有处理，但是也有没有参数进入该方法的时候
    		$name="empty";
    	}
		if(empty($department)){
    		$department=0;
    	}
		if(empty($type)){
    		$type=-1;
    	}
		if(empty($security)){
    		$security=-1;
    	}
		if(empty($fund)){
    		$fund=-1;
    	}
		if(empty($state)){
    		$state=-1;
    	}
		
		$data ['department']=$department;
		$data ['type']=$type;
		$data ['security']=$security;
		$data ['fund']=$fund;
		$data ['state']=$state;
		
		$this->load->library ( 'pagination' );
		// 每页显示数据条数
		$per_page=10;//暂定为10条
		
		$config['base_url']=site_url('staff/index/name/'.$name.'/department/'.$department.'/type/'.$type.'/security/'.$security.'/fund/'.$fund.'/state/'.$state);
		$this->load->model ( 'staff_model' );
		
		if($name=="empty"){//这里是为了搜索
    		$name=null;
    	}
		
		// 数据总数
		$config ['total_rows'] = $this->staff_model->count_staffs($name,$department,$type,$security,$fund,$state);
		//$config ['total_rows']=200; 
		$config ['per_page'] = $per_page;
		$config ['first_link'] = '第一页';
		$config ['last_link'] = '最后一页';
		$config ['next_link'] = '下一页';
		$config ['prev_link'] = '上一页';
		$config['uri_segment'] =15;
		
		$this->pagination->initialize($config);
		$data['links']=$this->pagination->create_links();
		//偏移量
		$offset=intval($this->uri->segment(15));
		$data ['staffs'] = $this->staff_model->find_staffs ($name,$department,$type,$security,$fund,$state,$per_page,$offset);
		$this->load->model ( 'department_model' );
		$data['departments']  = $this->department_model->find_all_departments();
		$data ['title'] = '员工列表 - 员工管理系统';
		$this->load->view ( 'header', $data );
		$this->load->view ( 'staff/index' );
		$this->load->view ( 'footer' );
	}
	/*
	 * 返回添加员工页面
	 */
	public function add() {
		$this->load->model ( 'department_model' );
		$data ['departments'] = $this->department_model->find_all_departments ();
		$data ['title'] = '添加员工 - 员工管理系统';
		$this->load->view ( 'header', $data );
		$this->load->view ( 'staff/add' );
		$this->load->view ( 'footer' );
	}
	/*
	 * 返回编辑员工页面
	 */
	public function edit() {
		$id = $this->uri->segment(4);
		$this->load->model ( 'staff_model' );
		$data ['staff'] = $this->staff_model->load ( $id );
		$this->load->model ( 'department_model' );
		$data ['departments'] = $this->department_model->find_all_departments ();
		$data ['title'] = '编辑员工 - 员工管理系统';
		$this->load->view ( 'header', $data );
		$this->load->view ( 'staff/edit' );
		$this->load->view ( 'footer' );
	}
	/*
	 * 保存或者更新员工的信息
	 */
	public function save() {
		$id = $this->input->post('id');
		$staff = array (
				'name' => $this->input->post ( 'name' ),
				'gender' => $this->input->post ( 'gender' ),
				'idcard' => $this->input->post ( 'idcard' ),
				'birthday' => $this->input->post ( 'birthday' ),
				'degree' => $this->input->post ( 'degree' ),
				'address' => $this->input->post ( 'address' ),
				'bankcard' => $this->input->post ( 'bankcard' ),
				'phone' => $this->input->post ( 'phone' ),
				'departmentid' => $this->input->post ( 'department' ),
				'type' => $this->input->post ( 'type' ),
				'security' => $this->input->post ( 'security' ),
				'fund' => $this->input->post ( 'fund' ),
				'insurance' => $this->input->post ( 'insurance' ),
				'state' => $this->input->post ( 'state' ),
				'start_date' => $this->input->post ( 'start' ),
				'end_date' => $this->input->post ( 'end' ),
				'salary' => $this->input->post ( 'salary' )
		);
		$this->load->model ( 'staff_model' );
		if ($id) {
			if($this->staff_model->update ( $id, $staff )){
				return_json(TRUE,'','修改成功！'); 
			}else{
				return_json(FALSE,'','信息未改动！');
			}
		} else {
			$staffid=$this->staff_model->create ( $staff );
			if(!empty($staffid)){
				return_json(TRUE,'','添加成功！'); 
			}else{
				return_json(FALSE,'','添加失败！');
			}
		}
	}
	/*
	 * 删除员工信息
	 */
	public function delete() {
		$id = $this->input->post('id');
		$this->load->model ( 'staff_model' );
		if ($this->staff_model->delete ( $id )) {
			return_json(TRUE,'','删除成功！'); 
		}else{
			return_json(TRUE,'','删除失败！');
		}
	}
	 /*
	 * 导出商品列表
	 */ 
    public function export() {
    	$resultPHPExcel = new PHPExcel ();
    	$resultPHPExcel->getActiveSheet ()->setCellValue ( 'A1', '姓名' );
    	$resultPHPExcel->getActiveSheet ()->setCellValue ( 'B1', '性别' );
    	$resultPHPExcel->getActiveSheet ()->setCellValue ( 'C1', '身份证号码' );
		$resultPHPExcel->getActiveSheet ()->setCellValue ( 'D1', '出生日期' );
    	$resultPHPExcel->getActiveSheet ()->setCellValue ( 'E1', '学历' );
    	$resultPHPExcel->getActiveSheet ()->setCellValue ( 'F1', '住址' );
		$resultPHPExcel->getActiveSheet ()->setCellValue ( 'G1', '类型' );
    	$resultPHPExcel->getActiveSheet ()->setCellValue ( 'H1', '银行卡号' );
    	$resultPHPExcel->getActiveSheet ()->setCellValue ( 'I1', '手机号码' );
		$resultPHPExcel->getActiveSheet ()->setCellValue ( 'J1', '职位' );
		$resultPHPExcel->getActiveSheet ()->setCellValue ( 'K1', '是否缴纳社保' );
		$resultPHPExcel->getActiveSheet ()->setCellValue ( 'L1', '是否缴纳公积金' );
    	$resultPHPExcel->getActiveSheet ()->getStyle('A1:L1')->getFont()->setBold(true);
    	$this->load->model ( 'staff_model' );
    	$staffs = $this->staff_model->find_all_staffs ();
		$i = 2;
		foreach ( $staffs as $staff ) {
			$resultPHPExcel->getActiveSheet ()->setCellValue ( 'A' .$i, $staff ['name']);
			if($staff ['gender']==0){
				$resultPHPExcel->getActiveSheet ()->setCellValue ( 'B' .$i, '男');
			}else{
				$resultPHPExcel->getActiveSheet ()->setCellValue ( 'B' .$i, '女');
			}
			$resultPHPExcel->getActiveSheet ()->setCellValueExplicit ( 'C' .$i, $staff ['idcard'],PHPExcel_Cell_DataType::TYPE_STRING);
			//$resultPHPExcel->getActiveSheet ()->getStyle('C'.$i)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
			$resultPHPExcel->getActiveSheet ()->setCellValue ( 'D' .$i, $staff ['birthday']);
			$resultPHPExcel->getActiveSheet ()->setCellValue ( 'E' .$i, $staff ['degree']);
			$resultPHPExcel->getActiveSheet ()->setCellValue ( 'F' .$i, $staff ['address']);
			if($staff ['type']==0){
				$resultPHPExcel->getActiveSheet ()->setCellValue ( 'G' .$i, '全职');
			}else{
				$resultPHPExcel->getActiveSheet ()->setCellValue ( 'G' .$i, '兼职');
			}
			$resultPHPExcel->getActiveSheet ()->setCellValueExplicit ( 'H' .$i, $staff ['bankcard'],PHPExcel_Cell_DataType::TYPE_STRING);
			$resultPHPExcel->getActiveSheet ()->setCellValue ( 'I' .$i, $staff ['phone']);
			$resultPHPExcel->getActiveSheet ()->setCellValue ( 'J' .$i, $staff ['dname']);
			if($staff ['security']==0){
				$resultPHPExcel->getActiveSheet ()->setCellValue ( 'K' .$i, '是');
			}else{
				$resultPHPExcel->getActiveSheet ()->setCellValue ( 'K' .$i, '否');
			}
			if($staff ['fund']==0){
				$resultPHPExcel->getActiveSheet ()->setCellValue ( 'L' .$i, '是');
			}else{
				$resultPHPExcel->getActiveSheet ()->setCellValue ( 'L' .$i, '否');
			}
			$i ++;
		}
    	//左对齐
    	$resultPHPExcel->getActiveSheet ()->getStyle ( 'A1:L' . $i )->getAlignment ()->setHorizontal ( PHPExcel_Style_Alignment::HORIZONTAL_LEFT );
    	// 设置列宽
    	$resultPHPExcel->getActiveSheet ()->getColumnDimension ( 'A' )->setWidth ( 10 );
    	$resultPHPExcel->getActiveSheet ()->getColumnDimension ( 'B' )->setWidth ( 8 );
    	$resultPHPExcel->getActiveSheet ()->getColumnDimension ( 'C' )->setWidth ( 20 );
		$resultPHPExcel->getActiveSheet ()->getColumnDimension ( 'D' )->setWidth ( 12 );
    	$resultPHPExcel->getActiveSheet ()->getColumnDimension ( 'E' )->setWidth ( 7 );
    	$resultPHPExcel->getActiveSheet ()->getColumnDimension ( 'F' )->setWidth ( 70 );
		$resultPHPExcel->getActiveSheet ()->getColumnDimension ( 'G' )->setWidth ( 7 );
    	$resultPHPExcel->getActiveSheet ()->getColumnDimension ( 'H' )->setWidth ( 20 );
    	$resultPHPExcel->getActiveSheet ()->getColumnDimension ( 'I' )->setWidth ( 15 );
		$resultPHPExcel->getActiveSheet ()->getColumnDimension ( 'J' )->setWidth ( 15 );
    	$resultPHPExcel->getActiveSheet ()->getColumnDimension ( 'K' )->setWidth ( 15 );
    	$resultPHPExcel->getActiveSheet ()->getColumnDimension ( 'L' )->setWidth ( 20 );
    	$outputFileName = '员工列表.xls';
    	$xlsWriter = new PHPExcel_Writer_Excel5 ( $resultPHPExcel );
    	// ob_start(); ob_flush();
    	header ( "Content-Type: application/force-download" );
    	header ( "Content-Type: application/octet-stream" );
    	header ( "Content-Type: application/download" );
    	header ( 'Content-Disposition:inline;filename="' . $outputFileName . '"' );
    	header ( "Content-Transfer-Encoding: binary" );
    	header ( "Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
    	header ( "Last-Modified: " . gmdate ( "D, d M Y H:i:s" ) . " GMT" );
    	header ( "Cache-Control: must-revalidate, post-check=0, pre-check=0" );
    	header ( "Pragma: no-cache" );
    	$xlsWriter->save ( "php://output" );
    }
}